﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SilverlightTesting
{
    [TestClass]
    public class TestGameDrawer
    {
        const double Epsilon = 0.0001;

        [TestMethod]
        public void TestCanvasImport()
        {
            Canvas can = new Canvas();
            can.Tag = "this is imported canvas";

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();

            gd.Canvas = can;

            Assert.AreEqual("this is imported canvas", gd.Canvas.Tag.ToString());
            Assert.AreEqual(can, gd.Canvas);
        }

        [TestMethod]
        public void TestCanvasExport()
        {
            Canvas can = new Canvas();
            can.Tag = "this is exported canvas";
            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;

            Canvas new_can = gd.Canvas;
            Assert.AreEqual("this is exported canvas", new_can.Tag.ToString());
            Assert.AreEqual(new_can, gd.Canvas);
        }

        /// <summary>
        /// Проверяем количество созданных квадратов в канвасе (64)
        /// </summary>
        [TestMethod]        
        public void TestChessBoardDrawBackground0()
        {
            Canvas can = new Canvas();
            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ChessBoardDrawBackground();
            can = gd.Canvas;

            SolidColorBrush black = new SolidColorBrush(Color.FromArgb(255, 0, 202, 225));
            SolidColorBrush white = new SolidColorBrush(Color.FromArgb(255, 192, 247, 253));

            int i = 0;
            foreach (var child in can.Children)
            {                
                Rectangle rect = child as Rectangle;
                if (rect.Tag.ToString() == "Cell")
                {
                    i += 1;
                }
            }
            Assert.AreEqual(64, i);
        }

        /// <summary>
        /// Тестируем полотно на то, что каждая клетка имеет верный цвет
        /// </summary>
        [TestMethod]
        public void TestChessBoardDrawBackground1()
        {
            Canvas can = new Canvas();
            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ChessBoardDrawBackground();
            can = gd.Canvas;

            SolidColorBrush black = new SolidColorBrush(Color.FromArgb(255, 0, 202, 225));
            SolidColorBrush white = new SolidColorBrush(Color.FromArgb(255, 192, 247, 253));
            SolidColorBrush temp = new SolidColorBrush();
            
            foreach (var child in can.Children)
            {
                Rectangle rect = child as Rectangle;
                if (rect.Tag.ToString() == "Cell")
                {
                    // get center of rect
                    Point ij = new Point((int) (rect.Margin.Left / 60), (int) (rect.Margin.Top / 60));
                    if ((ij.X + ij.Y) % 2 == 0)
                    {
                        temp = (SolidColorBrush) rect.Fill;
                        Assert.AreEqual(white.Color, temp.Color);
                    }
                    else
                    {
                        temp = (SolidColorBrush) rect.Fill;
                        Assert.AreEqual(black.Color, temp.Color);
                    }
                }
            }
        }

        /// <summary>
        /// Очищаем подсветку
        /// </summary>
        [TestMethod]
        public void TestClearHighlights0()
        {
            Canvas can = new Canvas();
            Rectangle rect = new Rectangle();
            rect.Tag = "Highlight";            
            can.Children.Add(rect);

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ClearHighlights();
            can = gd.Canvas;

            int i = 0;
            foreach (var el in can.Children)
            {
                Rectangle temp = el as Rectangle;
                if (temp.Tag.ToString() == "Highlight")
                {
                    i += 1;
                }
            }

            Assert.AreEqual(0, i);
        }

        /// <summary>
        /// Очищаем подсветку, если нет подсветки
        /// </summary>
        [TestMethod]
        public void TestClearHighlights1()
        {
            Canvas can = new Canvas();
            Rectangle rect = new Rectangle();
            rect.Tag = "lalalala";
            can.Children.Add(rect);

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ClearHighlights();
            can = gd.Canvas;

            int i = 0;
            foreach (var el in can.Children)
            {
                Rectangle temp = el as Rectangle;
                if (temp.Tag.ToString() == "Highlight")
                {
                    i += 1;
                }
            }

            Assert.AreEqual(0, i);
        }

        /// <summary>
        /// Проверим что очистка подсветки не стирает других элементов
        /// </summary>
        [TestMethod]
        public void TestClearHighlights2()
        {
            Canvas can = new Canvas();
            Rectangle rect = new Rectangle();
            rect.Tag = "lalalala";
            can.Children.Add(rect);

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ClearHighlights();
            can = gd.Canvas;

            int i = 0;
            foreach (var el in can.Children)
            {
                Rectangle temp = el as Rectangle;
                if (temp.Tag.ToString() == "lalalala")
                {
                    i += 1;
                }
            }

            Assert.AreEqual(1, i);
        }

        /// <summary>
        /// Очищаем фигуры
        /// </summary>
        [TestMethod]
        public void TestClearAllFigures0()
        {
            Canvas can = new Canvas();
            Rectangle rect = new Rectangle();
            rect.Tag = "Chess";
            can.Children.Add(rect);

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ClearAllFigures();
            can = gd.Canvas;

            int i = 0;
            foreach (var el in can.Children)
            {
                Rectangle temp = el as Rectangle;
                if (temp.Tag.ToString() == "Chess")
                {
                    i += 1;
                }
            }

            Assert.AreEqual(0, i);
        }

        /// <summary>
        /// Очищаем фигуры
        /// </summary>
        [TestMethod]
        public void TestClearAllFigures1()
        {
            Canvas can = new Canvas();
            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ClearAllFigures();
            can = gd.Canvas;

            int i = 0;
            foreach (var el in can.Children)
            {
                Rectangle temp = el as Rectangle;
                if (temp.Tag.ToString() == "Chess")
                {
                    i += 1;
                }
            }

            Assert.AreEqual(0, i);
        }

        /// <summary>
        /// Проверим что очистка фигур не стирает другие элементы
        /// </summary>
        [TestMethod]
        public void TestClearAllFigures2()
        {
            Canvas can = new Canvas();
            Rectangle rect = new Rectangle();
            rect.Tag = "lalalala";
            can.Children.Add(rect);

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            gd.ClearAllFigures();
            can = gd.Canvas;

            int i = 0;
            foreach (var el in can.Children)
            {
                Rectangle temp = el as Rectangle;
                if (temp.Tag.ToString() == "lalalala")
                {
                    i += 1;
                }
            }

            Assert.AreEqual(1, i);
        }

        /// <summary>
        /// Проверим что очистка фигур не стирает другие элементы
        /// </summary>
        [TestMethod]
        public void TestHasHiglightedCells0()
        {
            Canvas can = new Canvas();
            Rectangle rect = new Rectangle();
            rect.Tag = "Highlight";
            can.Children.Add(rect);

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            Assert.AreEqual(true, gd.HasHighlightedCells());
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void TestHasHiglightedCells1()
        {
            Canvas can = new Canvas();
            Rectangle rect = new Rectangle();
            rect.Tag = "lalala";
            can.Children.Add(rect);

            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            Assert.AreEqual(false, gd.HasHighlightedCells());            
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void TestHasHiglightedCells2()
        {
            Canvas can = new Canvas();
            ChessOnline.GameDrawer gd = new ChessOnline.GameDrawer();
            gd.Canvas = can;
            Assert.AreEqual(false, gd.HasHighlightedCells());
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void TestGetBoardCoordByMouse0()
        {
            ChessOnline.ChessOnlineService.Coordinate coord = new ChessOnline.ChessOnlineService.Coordinate();

            // a8
            coord.Literal = ChessOnline.ChessOnlineService.LiteralCoord.a;
            coord.Number = 8;
            ChessOnline.ChessOnlineService.Coordinate coord1 = ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(30, 30));
            Assert.AreEqual(coord.Literal, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(30, 30)).Literal);
            Assert.AreEqual(coord.Number, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(30, 30)).Number);


            // b3
            coord.Literal = ChessOnline.ChessOnlineService.LiteralCoord.b;
            coord.Number = 3;
            Assert.AreEqual(coord.Literal, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(90, 330)).Literal);
            Assert.AreEqual(coord.Number, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(90, 330)).Number);


            // h1
            coord.Literal = ChessOnline.ChessOnlineService.LiteralCoord.h;
            coord.Number = 1;
            Assert.AreEqual(coord.Literal, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(450, 450)).Literal);
            Assert.AreEqual(coord.Number, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(450, 450)).Number);
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void TestGetBoardCoordByMouse1()
        {
            Assert.AreEqual(null, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(1000, 1000)));
            //Assert.AreEqual(coord.Number, ChessOnline.GameDrawer.GetBoardCoordByMouse(new Point(1000, 1000)).Number);
        }
    }
}
